const express = require('express')
const router = express.Router()
const productService = require('../dao/book.dao')

const fs = require('fs')
router.get('/loading',(req,res)=>{
    fs.readFile('./other/images/loading.gif',(err,data)=>{
        res.write(data,'binary')
        res.end()
    })
})
router.get('/live2d',(req,res)=>{
    console.log(req.query.texturesId)
    // console.log(123)
    fs.readFile('./other/pio/model.json','utf8',(err,data)=>{
        data = JSON.parse(data)
        data.textures[0] = data.textures[req.query.texturesId];
        data = JSON.stringify(data)
        fs.writeFile('./other/pio/model2.json',data,'utf8',function () {
            console.log('修改成功')
        })
    })
    res.end()
})
router.get('/book',(req,res)=>{
    if(req.query.id){
        productService.listBook(req.query.id,(data)=>{
            data = JSON.stringify(data)
            data = JSON.parse(data)
            fs.readFile(data[0].img_url,(err,data)=>{
                res.write(data,'binary')
                res.end()
            })
        })
    }else{
        productService.listBook('',(data)=>{
            data = JSON.stringify(data)
            data = JSON.parse(data)
            res.json(data)
        })
    }
})
router.get('/book/advertisement',(req,res)=>{
    if(req.query.id){
        productService.listBook(req.query.id,(data)=>{
            data = JSON.stringify(data)
            data = JSON.parse(data)
            fs.readFile(data[0].advertisement_img_url,(err,data)=>{
                res.write(data,'binary')
                res.end()
            })
        })
    }else{
        productService.listBook('',(data)=>{
            data = JSON.stringify(data)
            data = JSON.parse(data)
            res.json(data)
        })
    }
})
router.get('/book/banner',(req,res)=>{
    if(req.query.id){
        productService.listBook_banner(req.query.id,(data)=>{
            data = JSON.stringify(data)
            data = JSON.parse(data)
            fs.readFile(data[0].img_url,(err,data)=>{
                res.write(data,'binary')
                res.end()
            })
        })
    }else{
        productService.listBook_banner('',(data)=>{
            data = JSON.stringify(data)
            data = JSON.parse(data)
            res.json(data)
        })
    }
})
router.get('/book/category',(req,res)=>{
    if(req.query.id){
        productService.listBook_category(req.query.id,(data)=>{
            data = JSON.stringify(data)
            data = JSON.parse(data)
            fs.readFile(data[0].img_url,(err,data)=>{
                res.write(data,'binary')
                res.end()
            })
        })
    }else{
        productService.listBook_category('',(data)=>{
            data = JSON.stringify(data)
            data = JSON.parse(data)
            res.json(data)
        })
    }
})


module.exports=router